﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CodeAuto.Pages
{
    public partial class TableEdit : System.Web.UI.Page
    {
        public Models.T_Table table = null;
        public List<Models.T_Field> fields = new List<Models.T_Field>();
        protected void Page_Load(object sender, EventArgs e)
        {
            DavidDB.ADO.DBManager db = new DavidDB.ADO.DBManager();
            table = new Models.T_Table();
            table.Id = Convert.ToInt32(Request["docid"]);
            table = db.GetModel(table);

            this.RegisterHyperLink.NavigateUrl = "/Pages/ProjectTables.aspx?docid=" + table.ProjectId;
            if (!IsPostBack)
            {


                Models.T_Field field = new Models.T_Field();
                field.TableId = table.Id;
                var query = db.GetSomeObject(field);
                foreach (Models.T_Field f in query)
                {
                    fields.Add(f);
                }

                this.TableName.Text = table.TableName;
                this.TextBox1.Text = table.Description;
                fields = fields.OrderBy(a => a.Id).ToList();

            }
            db.Commit();
            db.Dispose();
        }
        protected void LoginButton_Click(object sender, EventArgs e)
        {
            DavidDB.ADO.DBManager db = new DavidDB.ADO.DBManager();
            Models.T_Table table = new Models.T_Table();
            table.Id = Convert.ToInt32(Request["docid"]);
            table = db.GetModel(table);
            table.Description = this.TextBox1.Text;
            table.TableName = this.TableName.Text;
            db.Edit(table);


            Models.T_Field field = new Models.T_Field();
            field.TableId = table.Id;
            db.DeleteColumByKey_Value(field);

            for (int i = 0; i < 50; i++)
            {
                if (!string.IsNullOrEmpty(Request["f" + i]))
                {
                    Models.T_Field _field = new Models.T_Field();
                    _field.Attr = Request["add" + i];
                    _field.Field = Request["f" + i];
                    _field.FieldName = Request["n" + i];
                    _field.TableId = table.Id;
                    db.Insert(_field);
                }
            }

            db.Commit();
            db.Dispose();
            Response.Redirect("ProjectTables.aspx?docid=" + table.ProjectId);
        }
    }
}